/**
 常见简单宏的快捷应用
 */

#ifndef SpeedyMacros_h
#define SpeedyMacros_h

#pragma mark - 强弱引用&&代码块引用
//弱引用（代码块）
#define Weakself(type)  __weak typeof(type) weak##type = type;
//强引用（代码块）
#define Strongself(type)  __strong typeof(type) strong##type = type;
//代码块引用（代码块）
#define BlockSelf(type)  __block typeof(type) block##type = type;


#pragma mark - 判空

// 字符串
#define kIsEmptyStr(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
// 数组
#define kIsEmptyArr(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
// 字典
#define kIsEmptyDic(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
// 对象
#define kIsEmptyObj(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
// 空对象
#define kIsNull(str)     (!str||[str isKindOfClass:[NSNull class]])


#pragma mark - 设备尺寸获取&&系统版本获取&&尺寸简写
//设备宽高
#define kIphone_W [UIScreen mainScreen].bounds.size.width
#define kIphone_H [UIScreen mainScreen].bounds.size.height

//是否是iphoneX
#define kIsIPhoneX (kIphone_W>= 375.0f && kIphone_H >= 812.0f)

//底部安全高度
#define kBottomSafeH (kIsIPhoneX ? 34 : 0)
//tabbar高度
#define kTabbar_H (49 + kBottomSafeH)
//系统手势高度
#define kSystemGestureH (kIsIPhoneX ? 13 : 0)
/// 状态栏高度
#define KStatusBarH [[UIApplication sharedApplication] statusBarFrame].size.height
/// 导航栏高度
#define KNaviBarH (KStatusBarH + 44.f)

#define kFrame(x,y,w,h)         CGRectMake((x), (y), (w), (h))
#define kEdgeInsets(top,left,bottom,right) UIEdgeInsetsMake((top),(left),(left),(right))
#define kSize(w,h) CGSizeMake(w,h)
#define kPoint(x,y)             CGPointMake((x), (y))
// 由角度转换弧度
#define kDegreesToRadian(x)      (M_PI * (x) / 180.0)
// 由弧度转换角度
#define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)

// 适配比例 iPhone6 为标准  等比例缩放宽高位置 4（320*480） 5（320*568）6（375*667）6+（414*736）
#define kScale_W(w) ((w)*kIphone_W/375)
#define kScale_H(h) [[UIScreen mainScreen] bounds].size.height/667*(H)
#define kScale_Frame(x,y,w,h)  CGRectMake(((x)*/375), ((y)*(kIphone_H == 480 ? 667:kIphone_H)/667), ((w)*kIphone_W/375), ((h)*(kIphone_H == 480 ? 667:kIphone_H)/667))


/// 系统判断
#define kIsIOS7Later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
#define kIsIOS8Later ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
#define kIsIOS9Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f)
#define kIsIOS9_1Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.1f)
#define kIsIOS11Later ([UIDevice currentDevice].systemVersion.floatValue >= 11.0f)
#define kIsIOS13Later ([UIDevice currentDevice].systemVersion.floatValue >= 13.0f)
/// 机型判断
#define kIsIPhone4_4S                (kIphone_W==320.000000&&kIphone_H==480.000000)
#define kIsIPhone5_5S                (kIphone_W==320.000000&&kIphone_H==568.000000)
#define kIsIPhone6_6S                (kIphone_W==375.000000&&kIphone_H==667.000000)
#define kIsIPhone6Plus_6SPlus        (kIphone_W==414.000000&&kIphone_H==736.000000)




#pragma mark - 简写（包括获取图片、链接、字体设置、颜色设置、系统单例）
// 获取Window
#define kWindow [UIApplication sharedApplication].delegate.window
// Application
#define kApplication        [UIApplication sharedApplication]
// AppDelegate对象
#define kAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
// UserDefaults
#define kUserDefaults       [NSUserDefaults standardUserDefaults]
// NotificationCenter
#define kNotificationCenter [NSNotificationCenter defaultCenter]

#define kImage(imgStr) [UIImage imageNamed:imgStr]
#define kImgURL(imageStr) [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageStr]]]
#define kFileImage(imgStr) [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imgStr]]
//获取图片【本色】
#define kOriImage(imgStr) [[UIImage imageNamed:imgStr] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

#define kUrlFileData(fileUrl) [NSData dataWithContentsOfURL:[NSURL URLWithString:fileUrl]]
#define kLocalFileData(file) [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:file]]

//获取url链接
#define kURL(urlStr) [NSURL URLWithString:urlStr]
//快速字符串
#define kQuickNSString(str1,str2)      [NSString stringWithFormat:str1,str2]


#pragma mark-   weakify( x ) && strongify( x )
/// block self
#ifndef    weakify
#if __has_feature(objc_arc)

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \
_Pragma("clang diagnostic pop")

#else

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __block __typeof__(x) __block_##x##__ = x; \
_Pragma("clang diagnostic pop")

#endif
#endif

#ifndef    strongify
#if __has_feature(objc_arc)

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __weak_##x##__; \
_Pragma("clang diagnostic pop")

#else

#
#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __block_##x##__; \
_Pragma("clang diagnostic pop")

#endif
#endif

#pragma mark- 宏定义单粒
// .h
#define SingletonH(class) + (instancetype)shared##class;

// .m
#define SingletonM(class) \
static class *_instance; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
\
return _instance; \
} \
\
+ (instancetype)shared##class \
{ \
if (_instance == nil) { \
_instance = [[class alloc] init]; \
} \
\
return _instance; \
}\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}

#pragma mark- ZMLog
#ifdef DEBUG
#define ZMLog(format, ...) printf("\n[%s] %s [第%d行] %s\n", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ## __VA_ARGS__] UTF8String]);
#define JJLog(format, ...) NSLog(format, ## __VA_ARGS__)
#else
#define ZMLog(format, ...)
#define JJLog(format, ...)
#endif

//获取当前window的根控制器,如果获取不到,则通过响应链获取上一级控制器(必须在UIView或UIViewController及其子类中使用此宏)
#define kRootViewController ^UIViewController *{\
if ([UIApplication sharedApplication].keyWindow.rootViewController.view.window == nil) {\
UIResponder *nextResponder = self.nextResponder;\
NSInteger i = 0;\
while (YES) {\
if ([nextResponder isKindOfClass:[UIViewController class]]) {\
return (UIViewController *)nextResponder;\
break;\
}else if(nextResponder.nextResponder){\
nextResponder = nextResponder.nextResponder;\
}else{\
return nil;\
}\
i ++;\
}\
}else{\
return [UIApplication sharedApplication].keyWindow.rootViewController;\
}\
}()

#endif /* SpeedyMacros_h */
